package org.example;

import java.io.*;
import java.util.HashMap;

public class IOUtil {
    public static void main(String[] args) {
//        IOUtil ioUtil = new IOUtil();
//        ioUtil.IOToUser();
//        System.out.println(ioUtil.IOToUser());
    }

    /**
     * 将map集合进行序列化
     *
     * @param map allUser的map集合
     */
    public void UserToIO(HashMap map) {
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream("/Users/cengyunke/Desktop/atm1/atm/src/main/resources/User.txt"));
            objectOutputStream.writeObject(map);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 将序列化的文件转为allUser对象的Map集合
     *
     * @return allUser对象的Map集合
     */
    public HashMap IOToUser() {
        HashMap map;
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream("/Users/cengyunke/Desktop/atm1/atm/src/main/resources/User.txt"));
            map = (HashMap) objectInputStream.readObject();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return map;
    }
}
